package com.superteam.zzl.viewmodel.DynamicFragment;

import android.databinding.ViewDataBinding;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;

import com.superteam.zzl.activity.databinding.DynamicfragmentLayoutBinding;
import com.superteam.zzl.adapter.DynamicFragmentVpAdapter;
import com.superteam.zzl.adapter.RecyclerViewBaseAdapter;
import com.superteam.zzl.model.DynamicFragment.MDynamic;
import com.superteam.zzl.model.DynamicFragment.bean.Dynamic;
import com.superteam.zzl.view.fragment.MainDynamicFragment;
import com.superteam.zzl.view.implement.EventMainDynamicFragment;
import com.superteam.zzl.view.implement.IMainDynamicFragmentView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by Administrator on 2017/1/9.
 */

public class DynamicPresenter implements Callback<Dynamic>, ViewPager.OnPageChangeListener, TabLayout.OnTabSelectedListener, RecyclerViewBaseAdapter.OnItemClickLitener {

    private MainDynamicFragment fragment;
    private DynamicfragmentLayoutBinding db;
    private IMainDynamicFragmentView ifv;
    private MDynamic dynamic;
    private List<com.superteam.zzl.model.DynamicFragment.bean.Dynamic> list;
    private String[] titles;
    private DynamicFragmentVpAdapter adapter;
    private int position;
    private com.superteam.zzl.model.DynamicFragment.bean.Dynamic[] arrayDynamic;
    private int temp = 0;

    public DynamicPresenter(MainDynamicFragment fragment, IMainDynamicFragmentView ifv) {
        this.fragment = fragment;
        this.ifv = ifv;
        dynamic = new MDynamic(this);

    }

    public void initData() {
        dynamic.loadData();
    }



    public void Binding(ViewDataBinding db) {
        this.db = (DynamicfragmentLayoutBinding) db;
    }

    public void init() {
        list = new ArrayList<>();
        arrayDynamic = new com.superteam.zzl.model.DynamicFragment.bean.Dynamic[5];
        titles = dynamic.getTitles();
        adapter = new DynamicFragmentVpAdapter(fragment.getContext(), list, titles, this);
    }

    public void initView() {
        db.vp.addOnPageChangeListener(this);
        db.vp.setAdapter(adapter);
        // 监听和关联只能实现一个
//        db.tl.setOnTabSelectedListener(this);
        db.tl.setupWithViewPager(db.vp);
    }

    @Override
    public void onResponse(Call<com.superteam.zzl.model.DynamicFragment.bean.Dynamic> call, Response<com.superteam.zzl.model.DynamicFragment.bean.Dynamic> response) {
        arrayDynamic[temp] = response.body();
        temp++;
        if (temp == 5) {
            List<com.superteam.zzl.model.DynamicFragment.bean.Dynamic> collection = Arrays.asList(arrayDynamic);
            list.addAll(collection);
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public void onFailure(Call<com.superteam.zzl.model.DynamicFragment.bean.Dynamic> call, Throwable t) {
        Log.e("DynamicPresenter", "onFailure: " + "加载错误" + t.getMessage());
    }

    public com.superteam.zzl.model.DynamicFragment.bean.Dynamic.DataBean.TrendsListBean sendData(int tag) {
        List<com.superteam.zzl.model.DynamicFragment.bean.Dynamic.DataBean.TrendsListBean> trends_list = list.get(position).data.trends_list;
        return trends_list.get(tag);
    }

    @Override
    public void onTabSelected(TabLayout.Tab tab) {
        Log.e("DynamicPresenter", "onTabSelected: " + tab.getPosition());

    }

    @Override
    public void onTabUnselected(TabLayout.Tab tab) {

    }

    @Override
    public void onTabReselected(TabLayout.Tab tab) {

    }

    @Override
    public void onItemClick(View view, int position) {

    }

    @Override
    public boolean onItemLongClick(View view, int position) {
        return false;
    }

    public EventMainDynamicFragment setEvent() {
        return fragment;
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        Log.e("DynamicPresenter", "onPageSelected: " + position);
        this.position = position;
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
}
